﻿var SelectareTipConcediuPage = function(pageContainer) {

    SelectareTipConcediuPage.superclass.constructor.call(this, pageContainer);

    var self = this;
    var tipuriConcedii = new Array();
    var currentCallback = null;

    this.AllowSubtipuri = true;

    var Elements = {
        list: pageContainer.find(".list"),
        loader: pageContainer.find(".loadMore")
    };

    var Templates = {
        tipConcediuTemplate: pageContainer.find('.tipConcediuTemplate'),
        tipConcediuTemplateCompiled: null
    };

    self.initialize = override(self.initialize, function() {
        self.Title = translate('Module_Title_SelectareTipConcediu');
        app.translatePage(app.Pages.SelectareTipConcediu);

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(true);
        });

        pageContainer.bind('onPageShow', function() {
            if (!tipuriConcedii.length)
                self.refresh();
        });
    });

    this.setCallback = function(callback) {
        currentCallback = callback;
    };

    this.refresh = function(callback) {
        Elements.loader.show();
        app.TMMService.Cereri.GetTipuriConcedii(function(response) {
            if (response.Exception)
                Elements.loader.hide();
            else {
                tipuriConcedii = response.FinalResult;
                constructList(response.FinalResult);
            }
        });
    };

    var constructList = function(data) {
        if (!data)
            data = new Array();

        if (!Templates.tipConcediuTemplateCompiled) {
            var source = Templates.tipConcediuTemplate.html();
            Templates.tipConcediuTemplateCompiled = Handlebars.compile(source);
        }

        var html = "";
        for (var i = 0; i < data.length; i++) {
            var tc = data[i];
            tc.Nume = tc.TCO_NUME ? tc.TCO_NUME : tc.ELP_NUME;
            if (!self.AllowSubtipuri)
                tc.IsSpecial = false;
            html += Templates.tipConcediuTemplateCompiled(tc);
        }
        Elements.list.html(html);

        $(window).bind("hashchange", function() {
            if (location.hash == pageContainer.selector)
                self.CanGoBack = false;
        });

        Elements.list.find('.search-settings-arrow').click(function(e) {
            if (self.AllowSubtipuri) {
                if (!app.Pages.SelectareSubtipConcediu.instance)
                    app.Pages.SelectareSubtipConcediu.instance = new SelectareSubtipConcediu(app.Pages.SelectareSubtipConcediu.container);
                self.CanGoBack = true;
                app.changePage(app.Pages.SelectareSubtipConcediu);
                self.CanGoBack = false;

                var index = Elements.list.children().index($(this));
                app.Pages.SelectareSubtipConcediu.instance.refresh(tipuriConcedii[index], currentCallback);
            } else onElementSelected($(this));

            return Event.stopEvent(e);
        });

        Elements.list.find('.single').click(function(e) {
            var el = $(this);
            onElementSelected(el);
            return Event.stopEvent(e);
        });
        Elements.loader.hide();
    };

    var onElementSelected = function(el) {
        if (!currentCallback)
            return;
        self.CanGoBack = false;
        var index = Elements.list.children().index(el);
        currentCallback({ Tip: tipuriConcedii[index] });
    };

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    self.initialize();
};
extend(SelectareTipConcediuPage, TMMobilePage);